//
//  ViewController.swift
//  GradientLabel-Swift
//
//  Created by 万孟超 on 2022/1/19.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        installSubView()
    }

    func installSubView() {
        let imageView = UIImageView.init(image: UIImage.gradientColorImage(colors: ["ofc8ff".color(), "e08cff".color()], gradientType: .GradientLocationTypeTopToBottom, size: self.view.frame.size))
        imageView.frame = self.view.frame
        self.view.addSubview(imageView)
        let label = UIGradientLabel.init(frame: CGRect.init(x: 100, y: 300, width: self.view.frame.size.width - 200, height: 60))
        label.text = "这个文本的文字会进行变色扫描"
        label.font = UIFont.systemFont(ofSize: 18)
        label.textColor = .green
        label.textAlignment = .center
        label.numberOfLines = 0
        label.gradientColor = [.green, .green, .green, .green,"FF2E27".color(),"FC1AFF".color(), .green, .green, .green, .green]
        self.view.addSubview(label)
        label.startTextGradient()
    }
}

extension String {
    /// 十六进制字符串颜色转为UIColor
    /// - Parameter alpha: 透明度
    func color(alpha: CGFloat = 1.0) -> UIColor {
        // 存储转换后的数值
        var red: UInt64 = 0, green: UInt64 = 0, blue: UInt64 = 0
        var hex = self
        // 如果传入的十六进制颜色有前缀，去掉前缀
        if hex.hasPrefix("0x") || hex.hasPrefix("0X") {
            hex = String(hex[hex.index(hex.startIndex, offsetBy: 2)...])
        } else if hex.hasPrefix("#") {
            hex = String(hex[hex.index(hex.startIndex, offsetBy: 1)...])
        }
        // 如果传入的字符数量不足6位按照后边都为0处理，当然你也可以进行其它操作
        if hex.count < 6 {
            for _ in 0..<6-hex.count {
                hex += "0"
            }
        }
        // 分别进行转换
        // 红
        Scanner(string: String(hex[..<hex.index(hex.startIndex, offsetBy: 2)])).scanHexInt64(&red)
        // 绿
        Scanner(string: String(hex[hex.index(hex.startIndex, offsetBy: 2)..<hex.index(hex.startIndex, offsetBy: 4)])).scanHexInt64(&green)
        // 蓝
        Scanner(string: String(hex[hex.index(startIndex, offsetBy: 4)...])).scanHexInt64(&blue)

        return UIColor(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: alpha)
    }
}

extension UIImage {
    enum GradientLocationType {
        case GradientLocationTypeTopToBottom
        case GradientLocationTypeLeftToRight
        case GradientLocationTypeUpLeftToBottomRight
        case GradientLocationTypeUpRightToBottomLeft
    }
    static func gradientColorImage(colors : [UIColor], gradientType : GradientLocationType, size : CGSize ) -> UIImage? {
        var gradientColor = [CGColor]()
        for color in colors {
            gradientColor.append(color.cgColor)
        }
        UIGraphicsBeginImageContextWithOptions(size, true, 1)
        let context = UIGraphicsGetCurrentContext()
        context?.saveGState()
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let gradient = CGGradient.init(colorsSpace: colorSpace, colors: gradientColor as CFArray, locations: nil)
        let start : CGPoint?
        let end : CGPoint?
        switch gradientType {
        case .GradientLocationTypeTopToBottom:
            start = CGPoint.init(x: 0, y: 0)
            end = CGPoint.init(x: 0, y: size.height)
        case .GradientLocationTypeLeftToRight:
            start = CGPoint.init(x: 0, y: 0)
            end = CGPoint.init(x: size.width, y: 0)
        case .GradientLocationTypeUpLeftToBottomRight:
            start = CGPoint.init(x: 0, y: 0)
            end = CGPoint.init(x: size.width, y: size.height)
        case .GradientLocationTypeUpRightToBottomLeft:
            start = CGPoint.init(x: size.width, y: 0)
            end = CGPoint.init(x: 0, y: size.height)
        }
        context?.drawLinearGradient(gradient!, start: start!, end: end!, options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}
